package com.main.init;

import java.util.List;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.quartz.SchedulerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.main.bean.Task;
import com.main.quartz.QuartzUtil;
import com.main.service.TaskService;
import com.main.util.SpringContextUtil;

public class TaskContextListener  implements ServletContextListener{
   private Logger log=LoggerFactory.getLogger(this.getClass());
   
	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		log.info("定时任务摧毁");
	}

	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		log.info("定时任务初始化");
		TaskService ts=SpringContextUtil.getBean("taskService");
		List<Task>taskList=ts.getAllTask();
		for(Task t:taskList){
			try {
				if("1".equals(t.getQuartz_type())||"2".equals(t.getQuartz_type()))
				QuartzUtil.getQuartzUtil().addJob(t);
			} catch (SchedulerException e) {
				log.error("加载定时任务异常", e);
			} catch(Exception e){
				log.error("加载定时任务异常", e);
			}
		}
		log.info("定时任务初事化结束");
	}

	
}
